#include <iostream>
#include <vector>

// 统计时分多路复用
int multiplex(unsigned char* c, const int c_size, const unsigned char* a, const int a_len, const unsigned char* b, const int b_len) {
    int a_index = 0, b_index = 0;
    for (int i = 0; i < c_size; ++i) {
        if (a_index < a_len && a[a_index]) {
            c[i] = a[a_index++];
        } else if (b_index < b_len && b[b_index]) {
            c[i] = b[b_index++];
        } else {
            c[i] = 0;
        }
    }
    return 0;
}

// 统计时分多路解复用
int demultiplex(unsigned char* a, const int a_size, unsigned char* b, const int b_size, const unsigned char* c, const int c_len) {
    int a_index = 0, b_index = 0;
    for (int i = 0; i < c_len; ++i) {
        if (c[i]) {
            if (a_index < a_size) {
                a[a_index++] = c[i];
            } else if (b_index < b_size) {
                b[b_index++] = c[i];
            }
        }
    }
    return 0;
}

int main() {
    const int a_len = 5;
    const int b_len = 5;
    const int c_size = a_len + b_len;
    unsigned char a[a_len] = {1, 0, 1, 0, 1};
    unsigned char b[b_len] = {0, 1, 0, 1, 0};
    unsigned char c[c_size];
    unsigned char demux_a[a_len];
    unsigned char demux_b[b_len];

    // 多路复用
    multiplex(c, c_size, a, a_len, b, b_len);
    std::cout << "Multiplexed signal: ";
    for (int i = 0; i < c_size; ++i) {
        std::cout << static_cast<int>(c[i]) << " ";
    }
    std::cout << std::endl;

    // 解复用
    demultiplex(demux_a, a_len, demux_b, b_len, c, c_size);
    std::cout << "Demultiplexed signal a: ";
    for (int i = 0; i < a_len; ++i) {
        std::cout << static_cast<int>(demux_a[i]) << " ";
    }
    std::cout << std::endl;
    std::cout << "Demultiplexed signal b: ";
    for (int i = 0; i < b_len; ++i) {
        std::cout << static_cast<int>(demux_b[i]) << " ";
    }
    std::cout << std::endl;

    return 0;
}